var app = getApp();
var collectDataKey = 'collectDataList';
Page({
  data:{
    dataList:[],
    isEdit: false,
    lastDataList: [],
    pageNo: 1,
    pageSize: 8,
    totalDataList:[],
  },
  enterEditClick:function(e)
  {
     this.setData({
        isEdit: true,
        lastDataList: this.data.dataList,
     });
  },
  cancelEditClick:function(e)
  {
     this.setData({
        isEdit: false,
        dataList: this.data.lastDataList,
     });
  },
  collectClick:function(e)
  {
      this.data.dataList[parseInt(e.currentTarget.dataset.index)].collect = 1;
      this.setData({
         dataList : this.data.dataList,
      });
  },
  saveOperationClick:function(e)
  {
     //保存操作
     this.setData({
        isEdit: false,
     });
     wx.showNavigationBarLoading();
     wx.setStorage({
       key: collectDataKey,
       data: this.data.dataList,
       success: function(res){
         // success
       },
       fail: function() {
         // fail
       },
       complete: function() {
        setTimeout(wx.hideNavigationBarLoading,1000);
       }
     })
  },
  allCollectClick:function(e)
  {
     //全部收藏
     var dataList = this.data.dataList;
     for (var i = 0; i < dataList.length; i++)
     {
            dataList[i].collect = 1;
     }
     this.setData({
         dataList : this.data.dataList,
      });
  },
  cancelAllClick:function(e)
  {
     //取消所有收藏
     var dataList = this.data.dataList;
     for (var i = 0; i < dataList.length; i++)
     {
            dataList[i].collect = 0;
     }
     this.setData({
         dataList : this.data.dataList,
      });
  },
  loadMore:function(e)
  {
    var that = this;
    console.log('dd');
     wx.showNavigationBarLoading();
        //重新加载数据
        wx.request({
          url: 'https://apis.baidu.com/tngou/hospital/list',
          data: {
              id: 0,
              page: that.data.pageNo++,
              rows: 20,
          },
          header:{
            apikey: "da9ab87de033d3b01b34e35d5bfac404",
          },
          method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
          // header: {}, // 设置请求的 header
          success: function(res){
            that.setData({
              dataList:that.data.dataList.concat(res.data["tngou"]),
             });
          },
          fail: function() {
            // fail
          },
          complete: function() {
            // complete
           wx.stopPullDownRefresh();
           setTimeout(wx.hideNavigationBarLoading,500);
          }
        })

  },
  onPullDownRefresh:function(e)
  {
      var that = this;
     wx.showNavigationBarLoading();
        //重新加载数据
        wx.request({
          url: 'https://apis.baidu.com/tngou/hospital/list',
          data: {
              id: 10,
              page: 1,
              rows: 20,
          },
          header:{
            apikey: "da9ab87de033d3b01b34e35d5bfac404",
          },
          method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
          // header: {}, // 设置请求的 header
          success: function(res){
            that.setData({
              dataList:res.data["tngou"],
             });
          },
          fail: function() {
            // fail
          },
          complete: function() {
            // complete
           wx.stopPullDownRefresh();
           setTimeout(wx.hideNavigationBarLoading,500);
          }
        })
  },
  onLoad:function(options){
    // 页面初始化 options为页面跳转所带来的参数
    var that = this;
    // 从localStorage获取数据
    wx.showToast({
      icon:"loading",
      text:"loading..."
    });
    wx.getStorage({
      key: collectDataKey,
      success: function(res){
        if (res.data)
        {
          that.setData({
              dataList:res.data,
          });
        } else {
            deadSaveData();
        }
      },
      fail: function() {
        // fail
        deadSaveData();
      },
      complete: function() {
        // complete
          wx.hideToast();
      }
    });



    function deadSaveData()
    {
      var dataList = [
        {
            "address": "北京市宣武区右内大街11号",
            "area": 2,
            "count": 10272,
            "fax": "",
            "fcount": 0,
            "gobus": "<p> </p>",
            "id": 29650,
            "img": "/hospital/080709/7161081898f0d1e4a9e471c6a68199e9.jpg",
            "level": "其他",
            "mail": "",
            "mtype": "非医保",
            "name": "北京市西城区急救站",
            "nature": "政府举办非营利性",
            "rcount": 0,
            "tel": "63527755-260",
            "url": "http://bjxuanwu05207.11467.com/",
            "x": 116.364,
            "y": 39.882,
            "zipcode": "100055"
        },
        {
            "address": "北京市西城区平原里19号楼",
            "area": 2,
            "count": 6121,
            "fax": "",
            "fcount": 0,
            "gobus": "<p> 15、406、409、531、532、534、702、710、801路到希望广场下车或505、506路儿童医院下车。</p>",
            "id": 29649,
            "img": "/hospital/080619/252d470712abf097a793590b0c48bdca.jpg",
            "level": "其他",
            "mail": "",
            "mtype": "非医保",
            "name": "北京市西城区妇幼保健院",
            "nature": "政府举办非营利性",
            "rcount": 0,
            "tel": "",
            "url": "",
            "x": 116.373,
            "y": 39.8813,
            "zipcode": "100009"
        },
        {
            "address": "北京市东城区东长安街14号",
            "area": 2,
            "count": 3654,
            "fax": "",
            "fcount": 0,
            "gobus": "<p> </p>",
            "id": 29648,
            "img": "/hospital/080619/6fab47aa5a881e8eb386f0c3c97b8d2b.jpg",
            "level": "其他",
            "mail": "",
            "mtype": "居民医保",
            "name": "公安部机关门诊部",
            "nature": "非政府举办非营利性",
            "rcount": 0,
            "tel": "",
            "url": "",
            "x": 116.403,
            "y": 39.9074,
            "zipcode": "100005"
        },
        {
            "address": "无锡市学前东路宁海里（华东商贸城往西200米或学前东路与江海路交叉口） ",
            "area": 212,
            "count": 1679,
            "fax": "",
            "fcount": 0,
            "gobus": "<p> </p>",
            "id": 29647,
            "img": "/hospital/150806/91186748d153b9b14e6910f1d914611a.jpg",
            "level": "其他",
            "mail": "",
            "mtype": "居民医保",
            "name": "无锡港务医院(内科)",
            "nature": "",
            "rcount": 0,
            "tel": "0510-85136999",
            "url": "",
            "x": 120.332,
            "y": 31.587,
            "zipcode": "214000"
        },
        {
            "address": "襄阳市襄城南街陈侯巷169号",
            "area": 164,
            "count": 1571,
            "fax": "",
            "fcount": 0,
            "gobus": "<p> </p>",
            "id": 29646,
            "img": "/hospital/150806/e074a898933d01884346eed5b25da6a5.jpg",
            "level": "其他",
            "mail": "",
            "mtype": "居民医保",
            "name": "襄阳一0二医院",
            "nature": "",
            "rcount": 0,
            "tel": "0710-3551102",
            "url": "",
            "x": 112.157,
            "y": 32.0123,
            "zipcode": "湖北"
        },
        {
            "address": "海城市西关（原转盘旁）",
            "area": 235,
            "count": 1408,
            "fax": "",
            "fcount": 0,
            "gobus": "<p> </p>",
            "id": 29645,
            "img": "/hospital/150806/665292cb71ba138ddff66b48ab562bf0.jpg",
            "level": "其他",
            "mail": "",
            "mtype": "居民医保",
            "name": "海城阳光医院(妇科)",
            "nature": "",
            "rcount": 0,
            "tel": "0412-6155120",
            "url": "",
            "x": 122.513,
            "y": 40.9531,
            "zipcode": "114000"
        },
        {
            "address": "绵阳市涪城区长虹大道中段53号",
            "area": 318,
            "count": 1473,
            "fax": "0816-6019999",
            "fcount": 0,
            "gobus": "<p> 请乘坐1、7、8、19、20、23、33、60、80路公交车到图书馆站下车</p>",
            "id": 29644,
            "img": "/hospital/150806/6b70ed64a930045154e2bd8c0994642a.jpg",
            "level": "其他",
            "mail": "270719999@qq.com",
            "mtype": "居民医保",
            "name": "绵阳华西益康医院(妇科)",
            "nature": "",
            "rcount": 0,
            "tel": "0816-6019999",
            "url": "http://www.myhxyy.com/",
            "x": 104.738,
            "y": 31.4738,
            "zipcode": "621000"
        },
        {
            "address": "大庆市萨尔图区友谊大街116号",
            "area": 151,
            "count": 1331,
            "fax": "",
            "fcount": 0,
            "gobus": "<p> </p>",
            "id": 29640,
            "img": "/hospital/080619/96d1dc8a6b56813ccef1ea9032379e1d.jpg",
            "level": "二级甲等",
            "mail": "",
            "mtype": "居民医保",
            "name": "大庆五官医院",
            "nature": "",
            "rcount": 0,
            "tel": "0459-5817016",
            "url": "",
            "x": 125.002,
            "y": 46.6172,
            "zipcode": "163111"
        },
        {
            "address": "黑龙江省大庆市红岗区红岗中心村",
            "area": 151,
            "count": 1253,
            "fax": "",
            "fcount": 0,
            "gobus": "<p> </p>",
            "id": 29639,
            "img": "/hospital/150806/ab0f04fb268965a97c53baa930cff01f.jpg",
            "level": "二级乙等",
            "mail": "",
            "mtype": "居民医保",
            "name": "大庆市红岗区人民医院",
            "nature": "",
            "rcount": 0,
            "tel": "0459-4199510",
            "url": "",
            "x": 124.078,
            "y": 45.9671,
            "zipcode": "163000"
        },
        {
            "address": "黑龙江省大庆市龙凤区建安医院",
            "area": 151,
            "count": 1215,
            "fax": "",
            "fcount": 0,
            "gobus": "<p> </p>",
            "id": 29638,
            "img": "/hospital/080619/078ff104615e77020648d9295f58d6e3.jpg",
            "level": "一级甲等",
            "mail": "",
            "mtype": "居民医保",
            "name": "大庆市龙凤区建安医院",
            "nature": "",
            "rcount": 0,
            "tel": "0459-6244810",
            "url": "",
            "x": 125.135,
            "y": 46.5295,
            "zipcode": "163000"
        },
        {
            "address": "黑龙江省大庆市龙凤区龙凤大街8号",
            "area": 151,
            "count": 1225,
            "fax": "",
            "fcount": 0,
            "gobus": "<p> </p>",
            "id": 29637,
            "img": "/hospital/080619/6120f01ba37ffd7dd8c911abfe7398af.png",
            "level": "二级乙等",
            "mail": "",
            "mtype": "居民医保",
            "name": "大庆市龙凤区人民医院",
            "nature": "",
            "rcount": 0,
            "tel": "0459-6243535,6242872",
            "url": "",
            "x": 125.118,
            "y": 46.5339,
            "zipcode": "163711"
        },
        {
            "address": "黑龙江省大庆市让胡路区红卫星街三号",
            "area": 151,
            "count": 1217,
            "fax": "",
            "fcount": 0,
            "gobus": "<p> </p>",
            "id": 29636,
            "img": "/hospital/150806/2ae9edef184b3117f3e6c3f170064005.jpg",
            "level": "二级乙等",
            "mail": "",
            "mtype": "居民医保",
            "name": "大庆市让胡路区人民医院",
            "nature": "",
            "rcount": 0,
            "tel": "",
            "url": "",
            "x": 124.885,
            "y": 46.7011,
            "zipcode": "163000"
        },
        {
            "address": "大庆市让胡路区庆虹二路47号",
            "area": 151,
            "count": 1196,
            "fax": "",
            "fcount": 0,
            "gobus": "<p> </p>",
            "id": 29635,
            "img": "/hospital/080619/93e1316733bbfadbef01a06d588a25f2.jpg",
            "level": "二级甲等",
            "mail": "",
            "mtype": "居民医保",
            "name": "大庆市让胡路区铁路医院",
            "nature": "",
            "rcount": 0,
            "tel": "0459-5592637",
            "url": "",
            "x": 124.889,
            "y": 46.6591,
            "zipcode": "163712"
        },
        {
            "address": "石桥市红旗社区",
            "area": 239,
            "count": 1238,
            "fax": "",
            "fcount": 0,
            "gobus": "<p> </p>",
            "id": 29634,
            "img": "/hospital/080619/9a2e945438e6d28dbfb02cb34e367b75.jpg",
            "level": "二级甲等",
            "mail": "",
            "mtype": "居民医保",
            "name": "大石桥市陆合医院",
            "nature": "",
            "rcount": 0,
            "tel": "0417-5811582",
            "url": "",
            "x": 112.334,
            "y": 27.8594,
            "zipcode": "115100"
        },
        {
            "address": "长岛县城乐园街18号",
            "area": 279,
            "count": 1247,
            "fax": "",
            "fcount": 0,
            "gobus": "<p> 从长岛县政府向正南方向出发，沿长园路行驶450米，左转进入县府街，沿县府街行驶200米，右转进入解放路， 沿解放路行驶270米，过鸿达家电左转进入乐园大街， 沿乐园大街行驶390米，过右侧的乐园村，左转，行驶20米，到达终点。</p>",
            "id": 29633,
            "img": "/hospital/150806/c5841de753e6ef560ff985d5b8ce48c0.jpg",
            "level": "二级甲等",
            "mail": "",
            "mtype": "居民医保",
            "name": "长岛县人民医院",
            "nature": "",
            "rcount": 0,
            "tel": "0535-3212054,3212979",
            "url": "",
            "x": 120.743,
            "y": 37.9149,
            "zipcode": "265800"
        },
        {
            "address": "海滨路127号",
            "area": 279,
            "count": 1272,
            "fax": "",
            "fcount": 0,
            "gobus": "<p> 乘车在长岛县中医院下车即可。</p>",
            "id": 29629,
            "img": "/hospital/080619/f8c2a40a9b072f47c6ef458ec63cabd1.jpg",
            "level": "其他",
            "mail": "",
            "mtype": "居民医保",
            "name": "长岛县中医院",
            "nature": "",
            "rcount": 0,
            "tel": "0535-3211389",
            "url": "",
            "x": 116.716,
            "y": 23.3528,
            "zipcode": "265800"
        },
        {
            "address": "衡阳市公路大桥东桥头",
            "area": 187,
            "count": 1230,
            "fax": "",
            "fcount": 0,
            "gobus": "<p> </p>",
            "id": 29628,
            "img": "/hospital/080619/2db0aab138d6564ca92c4f71da1936c5.jpg",
            "level": "二级甲等",
            "mail": "",
            "mtype": "居民医保",
            "name": "衡阳市滨江医院衡阳市肝胆专科医院",
            "nature": "",
            "rcount": 0,
            "tel": "0734-8332506",
            "url": "",
            "x": 112.577,
            "y": 26.892,
            "zipcode": "421001"
        },
        {
            "address": "洪泽县朱坝镇新街90号",
            "area": 216,
            "count": 1236,
            "fax": "",
            "fcount": 0,
            "gobus": "<p> </p>",
            "id": 29627,
            "img": "/hospital/080619/319afcb34a310ef96e5707c21becd781.jpg",
            "level": "一级甲等",
            "mail": "",
            "mtype": "居民医保",
            "name": "洪泽县朱坝镇卫生院",
            "nature": "",
            "rcount": 0,
            "tel": "0517-7633015",
            "url": "",
            "x": 118.926,
            "y": 33.2825,
            "zipcode": "223113"
        },
        {
            "address": "辛集市东鹿大街采油五厂小区内",
            "area": 53,
            "count": 1242,
            "fax": "",
            "fcount": 0,
            "gobus": "<p> </p>",
            "id": 29626,
            "img": "/hospital/080619/df6b048af1d15aef3d94c9903803479c.jpg",
            "level": "一级甲等",
            "mail": "",
            "mtype": "居民医保",
            "name": "华北石油辛集矿区医院",
            "nature": "",
            "rcount": 0,
            "tel": "0311-82742281 ,0311-82742064 ",
            "url": "",
            "x": 113.699,
            "y": 22.9651,
            "zipcode": "052360"
        },
        {
            "address": "河北省廊坊市万庄石油矿区",
            "area": 127,
            "count": 1232,
            "fax": "",
            "fcount": 0,
            "gobus": "<p> </p>",
            "id": 29625,
            "img": "/hospital/080619/806881769ca685e51a8db7d9d481ddee.png",
            "level": "二级甲等",
            "mail": "",
            "mtype": "居民医保",
            "name": "华北石油廊坊矿区第一医院",
            "nature": "",
            "rcount": 0,
            "tel": "0317-6019753 ",
            "url": "",
            "x": 117.661,
            "y": 40.547,
            "zipcode": "065007"
        }
    ];
    wx.setStorageSync(collectDataKey, dataList);
    that.setData({
        dataList:dataList,
    });
    }
  },
  






  onReady:function(){
    // 页面渲染完成
    
  },
  onShow:function(){
    // 页面显示
    
  },
  onHide:function(){
    // 页面隐藏
    
  },
  onUnload:function(){
    // 页面关闭
    
  }
})